<?php
namespace app\index\controller;
use think\Controller;
//use app\admin\model\Category;
use think\Session;
use think\Validate;
use think\Db;
class Company extends Controller
{
    //显示企业主页面
    public function Index()
    {
         if(Session::get('usercate')==2){
            return view('index@company/index');
        }else{
           echo "<script> alert('抱歉,您不是企业用户!');parent.location.href='http://www.zzz.com/index/index/index'; </script>";
        }
    }
    //显示企业个人中心页面
    public function Center()
    {
        return view('index@company/add');

    }

    //显示修改密码页面
    public function Pwd()
    {
        return view('index@company/pwd');
    }

    //显示关于我们页面
    public function About()
    {
        return view('index@company/about');
    }



    //显示账户中心页面
    public function Recharge()
    {
        $companytel = Session::get('companytel');
        $id = Db::name('company')->field('company_id')->where('company_tel',$companytel)->find();
        $cid = $id['company_id'];
        $recharge = Db::name('company_recharge')->where('recharge_cid',$cid)->find();

        $this->assign('list',$recharge);
        return $this->fetch('index@company/recharge');
    }

    //显示账户充值页面
    public function Dorecharge()
    {
        $companytel = Session::get('companytel');
        $id = Db::name('company')->field('company_id')->where('company_tel',$companytel)->find();
        $cid = $id['company_id'];
        $recharge = Db::name('company_recharge')->field('recharge_money')->where('recharge_cid',$cid)->find();

        $this->assign('list',$recharge);
        return $this->fetch('index@company/dorecharge');
    }

    //显示职位管理页面
//    public function Position()
//    {
//        $data = Db::name('position')->paginate(10);
//        $count = Db::name('position')->count();
//        $this->assign('list',$data);
//        $this->assign('count',$count);
////        dump($data);die;
//        return $this->fetch('index@company/position');
//    }

    // 显示编辑页面
//    public function positionEdit($id)
//    {
//        $data = Db::name('position')->find($id);
//
//        $type = Db::name('category')->where('pid','1')->select();
//        $att = Db::name('category')->where('pid','2')->select();
//        $region = Db::name('category')->where('pid','3')->select();
////        dump($type); die;
//        $this->assign('data',$data);
//        $this->assign('type',$type);
//        $this->assign('att',$att);
//        $this->assign('region',$region);
//        return $this->fetch('company/part-edit');
//    }

    // 执行编辑
//    public function partEdit($id)
//    {
//
//        $data = input('post.');
////        dump($data);
//        $res['position_name'] = $data['name'];
//        $res['position_start'] = strtotime($data['start']);
//        $res['position_end'] = strtotime($data['end']);
//        $res['position_num'] = $data['num'];
//        $res['position_require'] = $data['require'];
//        $res['position_tel'] = $data['phone'];
//        $res['position_place'] = $data['place'];
//        $res['position_cateid'] = $data['type'].','.$data['att'].','.$data['region'];
////        dump($res); die;
//
//
//        $validate = new Validate([
//            'phone' => ['^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$']
//        ]);
//        $phone = [
//            'phone' => $data['phone']
//        ];
//        if (!$validate->check($phone)) {
//            dump($validate->getError());
//        }
//
//        $result = Db::name('position')->where('position_id',$id)->update($res);
//
//        if ($result == 0) {
//            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
//            return $this->error($result);
//        } else {
//            return $this->success($result);
//            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
//        }
//
//    }

    //显示添加
//    public function addPart()
//    {
//        $type = Db::name('category')->where('pid','1')->select();
//        $att = Db::name('category')->where('pid','2')->select();
//        $region = Db::name('category')->where('pid','3')->select();
//
//        $this->assign('type',$type);
//        $this->assign('att',$att);
//        $this->assign('region',$region);
//        return $this->fetch('company/part-add');
//    }

    // 执行 添加 职位
//    public function exeAdd()
//    {
//        $data = input('post.');
//        $res['position_name'] = $data['name'];
//        $res['position_start'] = strtotime($data['start']);
//        $res['position_end'] = strtotime($data['end']);
//        $res['position_num'] = $data['num'];
//        $res['position_require'] = $data['require'];
//        $res['position_tel'] = $data['phone'];
//        $res['position_place'] = $data['place'];
//        $res['position_cateid'] = $data['type'].','.$data['att'].','.$data['region'];
//        $res['position_publish'] = time();
//        $res['position_companyid'] = $data['companyid'];
//        $res['position_payway'] = $data['payway'];
//
//        $validate = new Validate([
//            'phone' => ['^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$']
//        ]);
//        $phone = [
//            'phone' => $data['phone']
//        ];
//        if (!$validate->check($phone)) {
//            dump($validate->getError());
//        }
//
//        $result = Db::name('position')->insert($res);
//
//        if ($result == 0) {
//            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
//            return $this->error($result);
//        } else {
//            return $this->success($result);
//            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
//        }
//
//    }

    //删除职位
//    public function delPart($id)
//    {
//        $result = Db::name('position')->delete($id);
//        if ($result > 0) {
////            $res = Db::name('position')->where('aid',$id)->delete($id);
//            $info['status'] = true;
//            $info['id'] = $id;
//            $info['info'] = 'ID为: ' . $id . '的职位删除成功!';
//        } else {
//            $info['status'] = false;
//            $info['id'] = $id;
//            $info['info'] = 'ID为: ' . $id . '的职位删除失败!';
//        }
//
//        return json($info);
//    }

    // 显示 兼职详情页面
//    public function roleMessage($id)
//    {
//
//        $data = Db::name('position')->where('position_id',$id)->find();
////        dump($data); die;
//        $data['position_start'] = date('Y-m-d',$data['position_start']);
//        $data['position_end'] = date('Y-m-d',$data['position_end']);
//        $data['position_publish'] = date('Y-m-d H:i:s',$data['position_publish']);
//
//        $this->assign('list',$data);
//        return $this->fetch('company/part-message');
//    }

    // 查看职位发布的状态
//    public function showStatus($status)
//    {
//        dump($status);
//    }

    //搜索功能查询列表
//    public function Search()
//    {
//       $data = input('get.');
////       return $data;die;
//       $name = $data['position_name'];
//       $start = strtotime($data['position_work1']);
//       $end = strtotime($data['position_work2']);
////       return $name.$start.$end;die;
//
////       return $res;
//       $res = Db::name('position')->where('position_name','like',"%$name%")->where('position_start','between',[$start,$end])->paginate(10);
////       dump($res);die;
//       $rows = Db::name('position')->where('position_name','like',"%$name%")->where('position_start','between',[$start,$end])->count();
//
//           $this->assign('list',$res);
//           $this->assign('count',$rows);
//           $this->fetch('index@company/position1');
//
//
//    }



    //执行编辑公司个人中心操作
    public function Edit()
    {
        $data = input('post.');
//        var_dump($data);die();

        //获取表单上传的文件
        $file = request()->file('company_icon');
//        dump($file);die();
        //移动到框架应用根目录下/public/uploads/目录下
        if($file){
            $info = $file->validate(['size'=>2097152,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){

                $savename = $info->getSaveName();
                $data['company_icon'] = str_replace('\\','/',$savename);
                $data['company_icon'] = '/uploads/'.$data['company_icon'];
            }else{
//                return $file->getError();
                echo "<script> alert('抱歉，未正确上传图片!');parent.location.href='http://www.zzz.com/index/company/center'; </script>";

            }
        }else{
            $data['company_icon'] = '/static/index/picture/photo2.jpg';
        }

        $validate = new Validate([
            'company_name'  => 'require|min:2',
            'company_leader'  => 'require|min:2',
            'company_qq'  => 'require|max:20',
            'company_address'  => 'require|min:2',
            'company_intro'  => 'require|min:2',
            'company_email'  => ['regex'=>'/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/'],
            'company_phone'  => 'require|number',
            'company_tele' => ['regex'=>'/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/']
        ]);

        $data['company_update'] = time();

        if (!$validate->check($data)) {
//            $arr = ['code'=>0,'msg'=>$validate->getError()];
            echo "<script> alert('抱歉，输入企业信息格式有误!');parent.location.href='http://www.zzz.com/index/company/center'; </script>";
        }else{
            $username = Db::name('company_information')->where(['company_name'=>$data['company_name']])->find();
            if($username){
//                $arr = ['code'=>0,'msg'=>'企业用户存在!'];
                echo "<script> alert('抱歉，企业用户存在!');parent.location.href='http://www.zzz.com/index/company/center'; </script>";
            }else{

                //获取该企业用户对象的id
                $companytel = Session::get('companytel');
                $cid = Db::name('company')->field('company_id')->where('company_tel',$companytel)->find();
//                var_dump($cid);die();

                $rows = Db::name('company_information')->where('company_cid',$cid['company_id'])->update($data);

                if($rows){
//                    $arr = ['code'=>0,'msg'=>'修改成功!'];
                     echo "<script> alert('恭喜，信息修改成功！');parent.location.href='http://www.zzz.com/index/company/center'; </script>";
                }else{
//                    $arr = ['code'=>0,'msg'=>'修改失败!'];
                    echo "<script> alert('抱歉，信息修改失败！');parent.location.href='http://www.zzz.com/index/company/center'; </script>";
                }
            }
        }

//        if ($arr['code'] == 0) {
//            return $this->error($arr['msg']);
//        } else {
//            return $this->success($arr['msg'],url('index/company/center'));
//             // echo "<script> alert('您不是企业用户');parent.location.href='http://www.zzz.com/index/company/center'; </script>";
//        }

    }


    //修改企业密码操作
    public function resetpwd()
    {
        $data = input('post.');
//        var_dump($data);die();
        $validate = new Validate([
            'oldpwd'  => 'require|min:2',
            'newpwd'  => 'require|min:2',

        ]);

        //获取该企业用户对象的pwd
        $companytel = Session::get('companytel');
        $pwd = Db::name('company')->field('company_pwd')->where('company_tel',$companytel)->find();
        if($pwd){
            if (password_verify($data['oldpwd'],$pwd['company_pwd'])) {
                if($data['newpwd'] == $data['renewpwd']){
                    $newdata['company_pwd'] = password_hash($data['newpwd'],PASSWORD_DEFAULT);
                    $newdata['company_regtime'] = time();
                    $rows = Db::name('company')->where('company_tel',$companytel)->update($newdata);
                    if ($rows){
//                        return $this->success('修改密码成功!');
                        echo "<script> alert('恭喜,修改密码成功!');parent.location.href='http://www.zzz.com/index/company/add'; </script>";
                    }else{
//                        return $this->error('修改密码失败!');
                        echo "<script> alert('抱歉,修改密码失败!');parent.location.href='http://www.zzz.com/index/company/center'; </script>";
                    }
                }else{
//                    return $this->error('两次密码输入不一致!');
                    echo "<script> alert('两次密码输入不一致!');parent.location.href='http://www.zzz.com/index/company/center'; </script>";
                }
            }else{
//                return $this->error('原密码输入不正确!');
                echo "<script> alert('原密码输入不正确!');parent.location.href='http://www.zzz.com/index/company/center'; </script>";

            }


        }

    }


    //企业账户充值操作
    public function Carry()
    {
        $data = input('post.');
        $money = $data['price'];
//        dump($data);die();
        $companytel = Session::get('companytel');
        $id = Db::name('company')->field('company_id')->where('company_tel',$companytel)->find();
        $cid = $id['company_id'];
        $rows = Db::name('company_information')->field('company_status')->where('company_cid',$cid)->find();

        if ($rows['company_status'] == 1){
            Session::set('status',$rows['company_status']);
            $recharge = Db::name('company_recharge')->field('recharge_money')->where('recharge_cid',$cid)->find();
            $money += $recharge['recharge_money'];
            $res = Db::name('company_recharge')->where('recharge_cid',$cid)->update(['recharge_money' => $money]);
            if ($res){
//                return $this->success('恭喜,充值成功!充值金额为:'.$data['price'].'元!');
                echo "<script> alert('恭喜,充值成功!充值金额为:'+{$data['price']}+'元!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";
            }else{
//                return $this->error('抱歉,充值失败!');
                echo "<script> alert('抱歉,充值失败!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";
            }

        }else{
//            return $this->error('请先进行企业认证!');
            echo "<script> alert('请先进行企业认证!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";
        }


    }


    //企业认证操作
    public function Attest()
    {
        $companytel = Session::get('companytel');
        $id = Db::name('company')->field('company_id')->where('company_tel',$companytel)->find();
        $cid = $id['company_id'];
        Session::set('recharge_cid',$cid);
        $res = Db::name('company_information')->field(array('company_icon','company_phone','company_qq'),true)->where('company_cid',$cid)->find();

        if($res){
            if ($res['company_status'] == 1){
                Session::set('status',$res['company_status']);
//                return $this->success('您已通过认证!');
                echo "<script> alert('您已通过认证!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";
            }else{
                $rows = Db::name('company_information')->where('company_cid',$cid)->update(['company_status' => 1]);
                $insert = Db::name('company_recharge')->insertGetId(['recharge_cid' => $cid]);
                if($rows && $insert){
                    Session::set('status',$res['company_status']);
//                    return $this->success('恭喜,通过认证!');
                    echo "<script> alert('恭喜,通过认证!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";

                }else{
//                    return $this->error('抱歉,认证失败!');
                    echo "<script> alert('抱歉,认证失败!');parent.location.href='http://www.zzz.com/index/company/center'; </script>";
                }

            }
        }else{
//            return $this->error('请先完善企业信息!');
            echo "<script> alert('请先完善企业信息!');parent.location.href='http://www.zzz.com/index/company/center'; </script>";
        }

    }


    //企业购买发布职位数量操作
    public function Jobpost()
    {
        $companytel = Session::get('companytel');
        $id = Db::name('company')->field('company_id')->where('company_tel',$companytel)->find();
        $cid = $id['company_id'];
        $res = Db::name('company_recharge')->field(['recharge_money','recharge_release'])->where('recharge_cid',$cid)->find();
//        dump($res);die();
        if($res['recharge_money'] >= 100){
            $res['recharge_money'] -= 100;
            $res['recharge_release'] += 1;
            $rows = Db::name('company_recharge')->where('recharge_cid',$cid)->update($res);
            if ($rows){
//                return $this->success('购买职位发布成功!');
                echo "<script> alert('购买职位发布成功!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";

            }else{
//                return $this->error('抱歉,购买失败!');
                echo "<script> alert('抱歉,购买失败!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";
            }
        }else{
//            return $this->error('余额不足,请充值!');
            echo "<script> alert('抱歉,余额不足,请充值!');parent.location.href='http://www.zzz.com/index/company/dorecharge'; </script>";
        }


    }


    //企业购买职位置顶次数操作
    public function Jobtop()
    {

        $companytel = Session::get('companytel');
        $id = Db::name('company')->field('company_id')->where('company_tel',$companytel)->find();
        $cid = $id['company_id'];
        $res = Db::name('company_recharge')->field(['recharge_money','recharge_topnum'])->where('recharge_cid',$cid)->find();
//        dump($res);die();
        if($res['recharge_money'] >= 10){
            $res['recharge_money'] -= 10;
            $res['recharge_topnum'] += 1;
            $rows = Db::name('company_recharge')->where('recharge_cid',$cid)->update($res);
            if ($rows){
//                return $this->success('购买职位置顶成功!');
                echo "<script> alert('购买职位置顶成功!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";
            }else{
//                return $this->error('抱歉,购买失败!');
                echo "<script> alert('抱歉,购买失败!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";
            }
        }else{
//            return $this->error('余额不足,请充值!');
            echo "<script> alert('抱歉,余额不足,请充值!');parent.location.href='http://www.zzz.com/index/company/dorecharge'; </script>";
        }

    }


    //企业购买查看简历数量操作
    public function Jobshow()
    {
        $companytel = Session::get('companytel');
        $id = Db::name('company')->field('company_id')->where('company_tel',$companytel)->find();
        $cid = $id['company_id'];
        $res = Db::name('company_recharge')->field(['recharge_money','recharge_viewnum'])->where('recharge_cid',$cid)->find();
//        dump($res);die();
        if($res['recharge_money'] >= 10){
            $res['recharge_money'] -= 10;
            $res['recharge_viewnum'] += 1;
            $rows = Db::name('company_recharge')->where('recharge_cid',$cid)->update($res);
            if ($rows){
//                return $this->success('购买查看简历成功!');
                echo "<script> alert('购买查看简历成功!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";

            }else{
//                return $this->error('抱歉,购买失败!');
                echo "<script> alert('抱歉,购买失败!');parent.location.href='http://www.zzz.com/index/company/recharge'; </script>";
            }
        }else{
//            return $this->error('余额不足,请充值!');
            echo "<script> alert('抱歉,余额不足,请充值!');parent.location.href='http://www.zzz.com/index/company/dorecharge'; </script>";
        }

    }

    //企业信息编辑功能
    public function Doedit()
    {
        $companytel = Session::get('companytel');
        $id = Db::name('company')->field('company_id')->where('company_tel',$companytel)->find();
        $cid = $id['company_id'];
        $res = Db::name('company_information')->where('company_cid',$cid)->find();
        $this->assign('list',$res);
        return $this->fetch('index@company/edit');
    }







}